package com.example.noobpackage.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.*
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.noobpackage.R
import kotlinx.android.synthetic.main.main_activity_layout.*
import kotlinx.android.synthetic.main.navigation_list.*

class MyPackage : AppCompatActivity() {

    // 配置ViewModel
    private val viewModel: MainViewModel by viewModels()

    // 配置Action Bar与抽屉的联动
    private lateinit var appBarConfiguration: AppBarConfiguration


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity_layout)

        // 配置应用抽屉
        appBarConfiguration = AppBarConfiguration(
            // 配置顶层页面，即会显示面包按钮的页面
            setOf(R.id.myPackageFragment, R.id.packageMarketFragment, R.id.proxyPackageFragment),
            drawerLayout
        )

        // 在Nav组件中获取跳转相关的Controller，让他接管actionBar的变化
        val host =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = host.navController

        // 将toolBar配置为actionBar
        setSupportActionBar(toolbar)

        setupActionBar(navController, appBarConfiguration)

        setupNavigationMenu(navController)

        // 添加lifecycle的观察器
        lifecycle.addObserver(MyPackageObserve(viewModel))

        //此处动态添加用户头像，xml中删除app:headerLayout
        val navigationHeaderView = navListView.inflateHeaderView(R.layout.navigation_list_header)
        val userIcon: ImageView = navigationHeaderView.findViewById(R.id.userIcon)
        val userName: TextView = navigationHeaderView.findViewById(R.id.userNameText)

        viewModel.userInfo.observe(this) {
            if (it == null) {
                userName.text = "未登录"
                userIcon.setImageResource(R.drawable.express_none)
            } else {
                userName.text = it.name!!
                userIcon.setImageResource(R.drawable.express_none)
                viewModel.updateOnlineExpress()
            }
        }

        //绑定头像事件
        userIcon.setOnClickListener {
            drawerLayout.close()
            if (viewModel.userInfo.value == null) {
                // 跳转到登录页
                findNavController(R.id.nav_host_fragment).navigate(R.id.loginFragment)
            } else {
                // 跳转到个人信息页
                findNavController(R.id.nav_host_fragment).navigate(R.id.userInfoFragment)
            }
        }

    }

    // 设置Action Bar的相关属性
    private fun setupActionBar(
        navController: NavController,
        appBarConfig: AppBarConfiguration
    ) {
        setupActionBarWithNavController(navController, appBarConfig)
    }

    // 将箭头的行为交给navController处理
    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
    }

    // 设置抽屉的导航
    private fun setupNavigationMenu(navController: NavController) {
        navListView?.setupWithNavController(navController)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return super.onOptionsItemSelected(item)
    }
}

class MyPackageObserve(private val viewModel: MainViewModel) : LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun activityResume() {
        Log.d("LifeCycle", "Resume")
    }
}